package com.liuyu.common.framework.common.utils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 地址解析工具
 * @author: liuyu
 * @date: 2021/9/15 11:23
 * @version: 1
 */
public class AddressResolutionUtil {
    public static final String REGEX = "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
    private static final Pattern PATTERN = Pattern.compile(REGEX);
    private static final String PROVINCE = "province";
    private static final String CITY = "city";
    private static final String COUNTY = "county";
    private static final String TOWN = "town";
    private static final String VILLAGE = "village";

    public static List<Map<String, String>> addressResolution(String address) {
        String province = null, city = null, county = null, town = null, village = null;
        Matcher m = PATTERN.matcher(address);
        List<Map<String, String>> table = new ArrayList<Map<String, String>>();
        Map<String, String> row = null;
        while (m.find()) {
            row = new LinkedHashMap<String, String>();
            province = m.group(PROVINCE);
            row.put(PROVINCE, province == null ? "" : province.trim());
            city = m.group(CITY);
            row.put(CITY, city == null ? "" : city.trim());
            county = m.group(COUNTY);
            row.put(COUNTY, county == null ? "" : county.trim());
            town = m.group(TOWN);
            row.put(TOWN, town == null ? "" : town.trim());
            village = m.group(VILLAGE);
            row.put(VILLAGE, village == null ? "" : village.trim());
            table.add(row);
        }
        return table;
    }

    public static void main(String[] args) {
        System.out.println(AddressResolutionUtil.addressResolution(""));
    }
}
